<template>
    <div class="plan01">
        <CrTitle>出行路线规划</CrTitle>
        <el-autocomplete
                class="cr-el-autocomplete"
                v-model="searchStr"
                :fetch-suggestions="querySearchAsync"
                placeholder="请输入内容"
                @select="handleSelect"
        >
            <template #prepend>
                <el-select v-model="selectMode" @change="handleSelect(0)" placeholder="请选择模式">
                    <el-option label="最快捷模式" value="0"></el-option>
                    <el-option label="最经济模式" value="1"></el-option>
                    <el-option label="最短距离模式" value="2"></el-option>
                    <el-option label="考虑实时路况" value="4"></el-option>
                </el-select>
            </template>
            <template #append>
                <el-button icon="el-icon-search" @click="handleSelect()"></el-button>
            </template>

        </el-autocomplete>
    </div>
    <div class="plan03" v-if="isSHowAnalysePlan">
        <CrTitle>交通状况分析</CrTitle>
        <ul class="infow-box">
            <li class="row" v-for="row in datalists">
                <div class="cont-box" v-for="item in row">
                    <div class="num">
                        <span class="icon" :style="{backgroundColor:item.bgc}"></span>
                        <span :style="{color:item.bgc}">{{item.num}}</span>
                    </div>
                    <div class="title">{{item.title}}</div>
                </div>
            </li>
        </ul>
        <div class="analyse-result-title">拥堵路段</div>
        <div class="tag-box">
            <div style="text-align: center;padding: 0.1rem;"
                 v-if="analyseResultDataRoad['拥堵'].length===0 && analyseResultDataRoad['严重拥堵']===0">没有拥堵路段
            </div>
            <el-tag v-for="item in analyseResultDataRoad['拥堵']" class="cr-el-tag" type="danger">{{item.road}}</el-tag>
            <el-tag v-for="item in analyseResultDataRoad['严重拥堵']" class="cr-el-tag" type="danger">{{item.road}}</el-tag>
        </div>
    </div>
    <div class="plan02">
        <div class="legend-box">
            <div class="legend">
                <span>畅通</span>
                <ul>
                    <li style="background-color: #34b000;"></li>
                    <li style="background-color: #fecb00;"></li>
                    <li style="background-color: #df0100;"></li>
                    <li style="background-color: #8e0e0b;"></li>
                </ul>
                <span>拥堵</span>
            </div>
        </div>
    </div>
</template>

<script setup>
    import CrTitle from "../../components/CrTitle.vue";
    import {onUnmounted, ref, computed } from 'vue'
    import {AMapUtil} from "../../utils/AMapUtil";
    import {GlobalVariables} from "../../GlobalVariables";
    import {AxiosCtrl} from "../../axios/index";
    import {JsUtil} from "../../utils/JsUtil";
    import {ElMessage} from 'element-plus'

    const searchStr = ref("");//搜索字符串
    const selectMode = ref("0");//选择模式
    const isSHowAnalysePlan = ref(false);//分析结果
    const analyseResultData = ref({
        "未知": [],
        "畅通": [],
        "缓行": [],
        "拥堵": [],
        "严重拥堵": [],
    });//分析结果
    const analyseResultDataRoad = ref({
      "未知": [],
      "畅通": [],
      "缓行": [],
      "拥堵": [],
      "严重拥堵": [],
    });//分析结果，名称不重复
    let saveItem = null;

    const datalists = ref([
        [
            {
                title: "总距离",
                bgc: "#ff4ff9",
                num: "0米",
            },
            {
                title: "预计时间",
                bgc: "#80c5ff",
                num: 0,
            }
        ],
        [
            {
                title: "严重拥堵",
                bgc: "#ff3e41",
                num: 0,
            },
            {
                title: "拥堵",
                bgc: "#ff3e41",
                num: 0,
            }
        ]
    ])

    async function querySearchAsync(queryString, cb) {
        console.log(queryString)
        if (!queryString) {
            cb([]);
            return;
        }
        const pois = await AxiosCtrl.searchByStr(queryString);
        const results = pois.map(item => {
            return {
                value: item.name,
                location: item.location
            };
        });
        cb(results);
    }

    function analyseResult(result, result2) {
        console.log(result2)
        datalists.value[0][0].num = result.distance + "米";
        datalists.value[0][1].num = JsUtil.formatSeconds(result.time);

        let dis = 0;
      for (let item of result2["严重拥堵"]) {
            dis += item.distance;
        }
        datalists.value[1][0].num = dis + "米";
        dis = 0;
        for (let item of result2["拥堵"]) {
            dis += item.distance;
        }
        datalists.value[1][1].num = dis + "米";
        analyseResultData.value = result2;
        isSHowAnalysePlan.value = true;
      analyseResultDataRoad.value["严重拥堵"] = JsUtil.arrayUnique(result2["严重拥堵"],"road");
      analyseResultDataRoad.value["拥堵"] = JsUtil.arrayUnique(result2["拥堵"],"road");
    }

    function handleSelect(item) {
        if (item) {
            saveItem = item;
        }
        if (!saveItem) {
            if (item === 0) {
                return;
            }
            ElMessage.warning("请先输入");
            return;
        }
        AMapUtil.Driving(GlobalVariables.curPosition, saveItem.location.split(","), parseInt(selectMode.value), analyseResult)
    }

    AMapUtil.onMapLoaded(() => {
        AMapUtil.showTraffic();
        GlobalVariables.map.setZoomAndCenter(14, GlobalVariables.curPosition);
    })

    onUnmounted(() => {
        AMapUtil.hideTraffic();
        AMapUtil.clearDriving();
    })
</script>

<style scoped lang="less">
    .plan01 {
        position: fixed;
        top: 0.8rem;
        left: 1.05rem;
        width: 4rem;
        background-color: #304a4f;

    }

    .plan03 {
        position: fixed;
        top: 2.8rem;
        left: 1.05rem;
        width: 4rem;
        background-color: #304a4f;
        .analyse-result-title {
            padding-left: 0.2rem;
            font-size: 0.2rem;
        }
        .tag-box {
            padding: 0.2rem;
            .cr-el-tag {
                margin: 0.05rem;
            }
        }

    }

    .plan02 {
        position: fixed;
        top: 0.8rem;
        right: 0.2rem;
        background-color: #304a4f;
        .legend-box {
            padding: 0 0.1rem;
            display: flex;
            justify-content: center;
            .legend {
                display: flex;
                align-items: center;
                justify-content: center;
                ul {
                    display: flex;
                    align-items: center;
                    padding: 0 0.1rem;
                    li {
                        width: 0.2rem;
                        height: 0.05rem;
                        background-color: #fff;
                        border-radius: 0.01rem;
                        margin: 0 0.01rem;
                    }
                }
            }
        }
    }
</style>
